﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Modding;
using Modding.Blocks;

namespace TrailBlockMod
{

    public class TrailBlockScript : BlockScript
    {

        TrailRenderer myTrailRenderer;
        ribbonScript ribbonScript;

        Renderer currentRenderer;

        MKey key_Enable;
        MToggle toggle_StartOnAwake;
        MToggle toggle_Hold;
        MSlider slider_LifeTime;
        MSlider slider_StartWidth;
        MSlider slider_EndWidth;

        MMenu menu_TrailShader;
        MToggle toggle_StartColor;
        MToggle toggle_EndColor;
        MSlider slider_R,slider_G,slider_B,slider_A;

        Color color_StartColor = Color.white;
        Color color_EndColor = Color.white;

        public override void SafeAwake()
        {

            key_Enable = AddKey(LanguageManager.Enable, "Enable", KeyCode.T);           
            toggle_StartOnAwake = AddToggle(LanguageManager.StartOnAwake, "start on awake", true);
            toggle_Hold = AddToggle(LanguageManager.Hold, "hold", false);
            slider_LifeTime = AddSlider(LanguageManager.LiftTime, "life time", 1f, 0.2f, 5f);
            slider_StartWidth = AddSlider(LanguageManager.StartWidth, "start width", 1f, 0.1f, 3f);
            slider_EndWidth = AddSlider(LanguageManager.EndWidth, "end width", 1f, 0.1f, 3f);

            //toggle_StartColor = AddToggle("开始颜色", "start color", true);
            //toggle_EndColor = AddToggle("结束颜色", "end color", false);
            menu_TrailShader = AddMenu("shader", 0, LanguageManager.ShaderItems);
            slider_R = AddSlider(LanguageManager.Red, "red", 255f, 0f, 255f);
            slider_G = AddSlider(LanguageManager.Green, "green", 255f, 0f, 255f);
            slider_B = AddSlider(LanguageManager.Blue, "blue", 255f, 0f, 255f);
            slider_A = AddSlider(LanguageManager.Alpha, "alpha", 255f, 0f, 255f);

            //key_Enable.DisplayInMapper = true;
            //toggle_StartOnAwake.DisplayInMapper = true;
            //toggle_Hold.DisplayInMapper = true;
            //slider_LifeTime.DisplayInMapper = true;
            //slider_StartWidth.DisplayInMapper = true;
            //slider_EndWidth.DisplayInMapper = true;"FX/Glass/Stained BumpDistort""Particles/Additive"

            

        }

        public override void OnBlockPlaced()
        {
            //Rigidbody.useGravity = false;
            Rigidbody.drag = Rigidbody.angularDrag = 0;
        }      

        public override void OnSimulateStart()
        {
            var color = new Color(slider_R.Value / 255f, slider_G.Value / 255f, slider_B.Value / 255f, slider_A.Value / 255f);

            if (menu_TrailShader.Value != 2)
            {
                myTrailRenderer = gameObject.AddComponent<TrailRenderer>();
                myTrailRenderer.autodestruct = false;
                myTrailRenderer.receiveShadows = false;
                myTrailRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

                myTrailRenderer.startWidth = slider_StartWidth.Value;
                myTrailRenderer.endWidth = slider_EndWidth.Value;
                myTrailRenderer.time = slider_LifeTime.Value;

               
                if (menu_TrailShader.Value == 0)
                {
                    myTrailRenderer.material = new Material(Shader.Find("Transparent/Diffuse"));
                    myTrailRenderer.material.SetColor("_Color", color);
                }
                else
                {
                    myTrailRenderer.material = new Material(Shader.Find("Particles/Additive"));
                    myTrailRenderer.material.SetColor("_TintColor", color);
                }

                //myTrailRenderer.material.mainTexture = resources["/TrailBlockMod/Trail.png"].texture;


                //myTrailRenderer.material.color = new Color32((byte)slider_R.Value, (byte)slider_G.Value, (byte)slider_B.Value, (byte)slider_A.Value);
                //myTrailRenderer.material.SetColor("_TintColor",
                //(Color)new Gradient()
                //{

                //    alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(color_StartColor.a, 0), new GradientAlphaKey(color_EndColor.a, slider_LifeTime.Value) },

                //    colorKeys = new GradientColorKey[] { new GradientColorKey(color_StartColor, 0), new GradientColorKey(color_EndColor, slider_LifeTime.Value) }

                //});

                currentRenderer = myTrailRenderer;
            }
            else
            {
 
                ribbonScript = gameObject.AddComponent<ribbonScript>();
                ribbonScript.Length = (int)slider_LifeTime.Value * 100;
                ribbonScript.StartWidth = slider_StartWidth.Value;
                ribbonScript.EndWidth = slider_EndWidth.Value;

                ribbonScript.material.shader = Shader.Find("Particles/Additive");
                ribbonScript.material.SetColor("_TintColor", color);
                ribbonScript.renderer.receiveShadows = false;
                ribbonScript.renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

                currentRenderer = ribbonScript.renderer;
            }
            currentRenderer.enabled = toggle_StartOnAwake.IsActive;
            foreach (var c in GetComponentsInChildren<BoxCollider>())
            {
                if (c.transform.parent.name == "Colliders")
                {
                    Destroy(c.gameObject);
                    break;
                }
            }

        }

        public override void SimulateUpdateHost()
        {

            if (toggle_StartOnAwake.IsActive)
            {
 
                judged(true);
            }
            else
            {           
                judged(false);
            }

            void judged(bool value)
            {         
                if (toggle_Hold.IsActive)
                {
                    if (key_Enable.IsDown)
                    {
                        currentRenderer.enabled = !value;
                    }
                    else
                    {
                        currentRenderer.enabled = value;
                    }
                }
                else
                {
                    if (key_Enable.IsPressed)
                    {
                        currentRenderer.enabled = !currentRenderer.enabled;
                    }
                }
            }

        }
    }
}
